<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增知识')" />
    <th:block th:include="include :: summernote-css" />
    <!--<th:block th:include="include :: bootstrap-fileinput-css" />-->
    <link th:href="@{/ajax/libs/layui/css/layui.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-knowledge-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">类型：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_sknowledge_type')}">
                    <input type="radio" th:id="${'type_' + dict.dictCode}" name="type" th:value="${dict.dictValue}" th:checked="${dict.default}" required>
                    <label th:for="${'type_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">标题：</label>
            <div class="col-sm-8">
                <input name="titel" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">

            <label class="col-sm-3 control-label is-required">封面：</label>
            <div class="col-sm-8">
                <input name="cover" id="cover" class="form-control" type="hidden" required>
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">内容：</label>
            <div class="col-sm-8">
                <input id="content" name="content" type="hidden">
                <div class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8" style="margin-top: 5px">
                <div class="">
                    正常 <input type="radio" name="status" value="0" checked>&nbsp;&nbsp;&nbsp;&nbsp;
                    关闭<input type="radio" name="status" value="1" >
                </div>
            </div>
        </div>


    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<!--<th:block th:include="include :: bootstrap-fileinput-js" />-->
<script th:src="@{/ajax/libs/layui/layui.all.js}"></script>
<script type="text/javascript">
    var prefix = ctx + "hr/knowledge"
    $("#form-knowledge-add").validate({
        focusCleanup: true
    });

    $('.summernote').summernote({
        placeholder: '请输入知识内容',
        height : 192,
        lang : 'zh-CN',
        followingToolbar: false,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function(result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function(error) {
                $.modal.alertWarning("文件上传失败。");
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {

            if ($.common.isEmpty($('#cover').val())){
                $.modal.alertWarning("请输上传封面图片。");
                return;
            }
            debugger

            var sHTML = $('.summernote').summernote('code');
            if (!sHTML || $.common.equals(sHTML,'<p><br></p>')){
                $.modal.alertWarning("请输入知识内容。");
                return;
            }
            $("#content").val(sHTML);

            // $("#fileinput-demo-1").fileinput("upload");

            $.operate.save(prefix + "/add", $('#form-knowledge-add').serialize());
        }
    }


   /* $(document).ready(function () {
        $("#fileinput-demo-1").fileinput({
            'theme': 'explorer-fas',
            'uploadUrl': ctx + "common/upload",
            allowedFileExtensions: ["jpg", "JPG", "jpeg", "JPEG", "png", "gif"],
            overwriteInitial: false,
            initialPreviewAsData: true,
            initialPreview: [
                "/img/profile.jpg"
            ]
        }).on("fileuploaded", function(event, data, previewId, index) {
            debugger
        });
    });*/


    layui.use('upload', function(){
        var $ = layui.jquery
            ,upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: ctx + "common/upload" //改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    debugger
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                $('#cover').val(res.fileName); //图片路径
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });

</script>
</body>
</html>